import { mapState, mapMutations } from 'vuex'
import { getBirthday,getNewToken,Trim,gender } from '../../config/util'
import { passengerMessVerificate } from '../../config/messVerify'
export default {
	name: 'editPassenger',
	data() {
		return {
			success: false,
			failed: false,
			type: false,             //默认为国内乘机人
			passenger: '',           //旅客类型默认为成人
			certificateDefault: '2',  
			sex: "",                 //默认为男
			chineseName: "",         //国内姓名字段
			docId: "",               //证件号
			mobile: "",              //国内乘机人的手机号码
			sureName: "",
			givenName: "",
			nationality: "",
			birthDate: "",
			fromDate: "",
			inlandMobile: '',
			docExpireDate: "",
			errorMsg:'修改失败',
			id:null,
		}
	},
	computed: {
		...mapState([
			'editPassenger', 'Date', 'isInland','insurance','token'
		]),
		certificate() {
			if(!this.type) this.certificateDefault = '1';
			else{
				if(this.certificateDefault=='1'){
					this.certificateDefault = '2'					
				}				
			}
			return this.certificateDefault;
		}
	},
	watch:{
		failed:function(val){
			if(val){
				var that = this;
				setTimeout(function(){
					that.failed = false;
				},5000)
			}
		},
		/*sureName:function(val){
			if(val) this.sureName = val.toUpperCase();			
		},
		givenName:function(val){
			if(val) this.givenName = val.toUpperCase();
		}*/
	},
	methods: {
		...mapMutations([
			'RECORD_PASSENGERLIST','RECORD_INSURANCE','RECORD_TOKEN'
		]),
		passengerType(value) {
			this.passenger = value
		},
		certificateType(value) {
			this.certificateDefault = value;
			if(value == "1") this.type = false;
			else this.type = true;
		},
		getParams() {
			var data;
			if(this.type) {
				this.sureName = (this.sureName).toUpperCase()
				this.givenName = (this.givenName).toUpperCase()
				data = {
					"firstNameCn": Trim(this.sureName+"/"+this.givenName),
					"passengerType": this.passenger,
					"docType": this.certificateDefault,
					"docId": Trim(this.docId),
					"docExpireDate": this.docExpireDate,
					"mobile": Trim(this.inlandMobile),
					"birthDate": this.birthDate,
					"fromDate": this.Date.postDepDate,
					"sex": this.sex,
					"nationality": Trim(this.nationality),
					"lastName_cn": Trim(this.sureName+"/"+this.givenName),
					"lastNameEn": Trim(this.givenName),
					"flightType": this.isInland ? "1" : "2",
					"id":this.id,
				};
			} else {
				data = {
					"firstNameCn": Trim(this.chineseName),
					"lastName_cn": Trim(this.chineseName),
					"passengerType": this.passenger,
					"docType": this.certificateDefault,
					"docId": Trim(this.docId),
					"birthDate": getBirthday(this.docId),
					"mobile": Trim(this.mobile),
					"flightType": this.isInland ? "1" : "2",
					"fromDate": this.Date.postDepDate,
					"id":this.id,
					'sex':gender(this.docId)
				};								
			}
			console.log(data)
			return data;
		},
		edit() {	
			var data = this.getParams();//获取参数
			var result = passengerMessVerificate(data);//校验
			if(!result.status){         //如果校验结果有误，展示原因 阻止请求
				this.errorMsg = result.msg;
				this.failed = true;
				return false;
			}		
			this.$axios({
					method: 'post',
					url: '/passenger/update?temp=' + Math.random(),
					data: this.getParams(),
					headers: {'authorization':this.token}
				})
				.then((res) => {
					this.RECORD_TOKEN({type:"update",val:getNewToken(res.headers)});
					var success = res.data.success;
					this.handResult(success);
				})
				.catch((error) => {
					console.log(error)
					alert("请求异常")
				})
		},
		handResult(success) {
			var that = this;
			if(success) {
				this.success = true;
				this.failed = false;
				this.RECORD_PASSENGERLIST({type:"edit",passenger:this.getParams()});
				
				if(this.insurance.length!=0){
					this.RECORD_INSURANCE({type:"edit",passenger:this.getParams()});
				}
				setTimeout(function() {
					that.$router.push({ path: '/book' });
				}, 1000)
			} else {
				this.success = false;
				this.failed = true;
			}
		},
		initialize(data) {
			this.type = data.docType == 1 ? false : true;
			this.certificateDefault = (data.docType).toString();
			this.passenger = (data.passengerType).toString();
			this.docId = data.docId;
			this.mobile = data.mobile;
			this.inlandMobile = data.mobile;
			this.chineseName = data.firstNameCn;

			this.sex = data.sex?data.sex:1;
			this.birthDate = data.birthDate;
			this.nationality = data.nationality;
			this.docExpireDate = data.docExpireDate?data.docExpireDate:"";
			this.sureName = data.firstNameCn.split("/")[0];
			this.givenName = data.firstNameCn.split("/")[1];
			this.id = data.id;
		},
	},
	created() {
		document.title = "修改乘机人";
		var data = JSON.parse(JSON.stringify(this.editPassenger));
		this.initialize(data); //初始化修改的乘客的基础信息
		//国际航班 只能添加国际乘机人
		if(!this.isInland){
			this.type = true;
		}
	}
}